package xyz.scootaloo.oj.offer

import org.junit.Test
import xyz.scootaloo.oj.ds.ListNode
import xyz.scootaloo.oj.ds.helper.CollectionHelper

/**
 * @author flutterdash@qq.com
 * @since 2021/4/10 22:02
 */
class P25 {

    private fun mergeTwoLists(l1: ListNode?, l2: ListNode?): ListNode? {
        if (l1 == null && l2 == null) return null
        val head = ListNode(-1)
        var hp = head
        var lp1 = l1
        var lp2 = l2
        while (lp1 != null || lp2 != null) {
            if (lp1 == null) {
                hp.next = lp2
                break
            }
            if (lp2 == null) {
                hp.next = lp1
                break
            }
            if (lp1.`val` < lp2.`val`) {
                hp.next = lp1
                lp1 = lp1.next
            } else {
                hp.next = lp2
                lp2 = lp2.next
            }
            hp = hp.next
        }
        return head.next
    }

    @Test
    fun test0() {
        val headA = ListNode(1)
        val headA2 = ListNode(2)
        val headA3 = ListNode(4)
        val headB = ListNode(1)
        val headB2 = ListNode(3)
        val headB3 = ListNode(4)
        headA.next = headA2
        headA2.next = headA3
        headB.next = headB2
        headB2.next = headB3
        CollectionHelper.showListNodes(mergeTwoLists(headA, headB))
    }

}
